package com.ruoyi.business.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TProductReviewMapper;
import com.ruoyi.business.domain.TProductReview;
import com.ruoyi.business.service.ITProductReviewService;
import com.ruoyi.business.domain.TOrder;
import com.ruoyi.business.service.ITOrderService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品评价Service业务层处理
 * 
 * @author laoguo
 * @date 2025-08-13
 */
@Service
public class TProductReviewServiceImpl implements ITProductReviewService 
{
    @Autowired
    private TProductReviewMapper tProductReviewMapper;

    @Autowired
    private ITOrderService orderService;

    /**
     * 查询商品评价
     * 
     * @param reviewId 商品评价主键
     * @return 商品评价
     */
    @Override
    public TProductReview selectTProductReviewByReviewId(Long reviewId)
    {
        return tProductReviewMapper.selectTProductReviewByReviewId(reviewId);
    }

    /**
     * 查询商品评价列表
     * 
     * @param tProductReview 商品评价
     * @return 商品评价
     */
    @Override
    public List<TProductReview> selectTProductReviewList(TProductReview tProductReview)
    {
        return tProductReviewMapper.selectTProductReviewList(tProductReview);
    }

    /**
     * 新增商品评价
     * 
     * @param tProductReview 商品评价
     * @return 结果
     */
    @Override
    public int insertTProductReview(TProductReview tProductReview)
    {
        tProductReview.setCreateTime(DateUtils.getNowDate());
        return tProductReviewMapper.insertTProductReview(tProductReview);
    }

    /**
     * 修改商品评价
     * 
     * @param tProductReview 商品评价
     * @return 结果
     */
    @Override
    public int updateTProductReview(TProductReview tProductReview)
    {
        tProductReview.setUpdateTime(DateUtils.getNowDate());
        return tProductReviewMapper.updateTProductReview(tProductReview);
    }

    /**
     * 批量删除商品评价
     * 
     * @param reviewIds 需要删除的商品评价主键
     * @return 结果
     */
    @Override
    public int deleteTProductReviewByReviewIds(Long[] reviewIds)
    {
        return tProductReviewMapper.deleteTProductReviewByReviewIds(reviewIds);
    }

    /**
     * 删除商品评价信息
     * 
     * @param reviewId 商品评价主键
     * @return 结果
     */
    @Override
    public int deleteTProductReviewByReviewId(Long reviewId)
    {
        return tProductReviewMapper.deleteTProductReviewByReviewId(reviewId);
    }

    @Override
    @Transactional // 建议加上事务
    public void submitReview(TProductReview review) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();

        // 步骤1：数据校验
        if (review.getOrderId() == null || review.getProductId() == null || review.getStars() == null) {
            throw new ServiceException("缺少必要的评价信息");
        }

        // 步骤2：校验订单的合法性
        TOrder order = orderService.selectTOrderByOrderId(review.getOrderId());
        if (order == null || !order.getUserId().equals(userId)) {
            throw new ServiceException("订单不存在或无权评价");
        }

        // 步骤3：校验订单状态是否允许评价
        // 假设 "3" 是已完成/待评价状态
        if (!"3".equals(order.getOrderStatus())) {
            throw new ServiceException("当前订单状态无法评价");
        }

        // 步骤4：校验用户是否已评价过此订单商品
        TProductReview queryParam = new TProductReview();
        queryParam.setOrderId(review.getOrderId());
        queryParam.setProductId(review.getProductId());
        queryParam.setUserId(userId);
        List<TProductReview> existReviews = tProductReviewMapper.selectTProductReviewList(queryParam);
        if (!existReviews.isEmpty()) {
            throw new ServiceException("您已评价过该商品");
        }

        // 步骤5：补充用户信息并插入数据库
        review.setUserId(userId);
        review.setUserNickname(loginUser.getUser().getNickName());
        review.setUserAvatar(loginUser.getUser().getAvatar());
        review.setCreateBy(loginUser.getUsername());
        review.setStatus("0"); // 默认显示

        tProductReviewMapper.insertTProductReview(review);

        // 步骤6：更新订单状态为“已评价”
        order.setOrderStatus("4");
        orderService.updateTOrder(order);
    }
}
